package cn.edu.hbvtc.qms.web.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.edu.hbvtc.qms.domain.EvalPeriod;
import cn.edu.hbvtc.qms.domain.Role;
import cn.edu.hbvtc.qms.domain.User;
import cn.edu.hbvtc.qms.service.IEvaluationService;
import cn.edu.hbvtc.qms.service.IManagementService;
import cn.edu.hbvtc.qms.web.struts.form.EvalPeriodActionForm;

public class EvalPeriodAction extends AbstractBaseDispatchAction {
	
	private IEvaluationService evaluationService = null;
	private IManagementService managementService = null;

	public IEvaluationService getEvaluationService() {
		return evaluationService;
	}

	public void setEvaluationService(IEvaluationService evaluationService) {
		this.evaluationService = evaluationService;
	}

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}
	
	//跳转到课程评价时间显示页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = managementService.findUserByUsername(this.getPrincipal().getUsername());
		Role role = managementService.findRoleByName("DEPT_ADMIN");
		
		EvalPeriod ep = null;
		
		//如果当前登录用户是院系管理员，则显示当前课程评价时间，并提示用户更新课程评价时间
		if (user.getRoles().contains(role)) {
			ep = evaluationService.findEvalPeriodByUserId(user.getId());
		}
		
		request.setAttribute("ep", ep);
		
		return mapping.findForward("update");
	}
	
	//保存评价时间设置数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = managementService.findUserByUsername(this.getPrincipal().getUsername());
		
		EvalPeriodActionForm epaf = (EvalPeriodActionForm) form;
		
		if (epaf.getStartDate() == null || "请选择".equals(epaf.getStartDate())) {
			request.setAttribute("startDateError", "请选择课程评价开始时间！");
			return mapping.findForward("update");
		}
		
		if (epaf.getEndDate() == null || "请选择".equals(epaf.getEndDate())) {
			request.setAttribute("endDateError", "请选择课程评价结束时间！");
			return mapping.findForward("update");
		}
		
		if (epaf.getStartDate().compareTo(epaf.getEndDate()) > 0) {
			request.setAttribute("startDateGTendDate", "评价结束时间小于评价开始时间，请重新选择！");
			return mapping.findForward("update");
		}
		
		//如果院系管理员当前未设置任何评价时间，则创建新的课程评价时间对象；
		//更正：如果已经设置评价时间，则将现有评价时间对象enabled置为0，并先增加当前评价时间对象
		if (epaf.getId() != 0) {
			EvalPeriod epAgo = evaluationService.findEvalPeriodById(epaf.getId());
			epAgo.setEnabled(0);
			evaluationService.updateEvalPeriod(epAgo);
		}
		
		EvalPeriod ep = null;
		ep = new EvalPeriod();
		ep.setDeptAdmin(user);
		ep.setDept(managementService.findTeacherByUserId(user.getId()).getDept());
		ep.setStartDate(epaf.getStartDate());
		ep.setEndDate(epaf.getEndDate());
		ep.setEnabled(1);
		ep.setTerm(managementService.findTermOfNow());
		evaluationService.saveEvalPeriod(ep);	

		return mapping.findForward("updateSave");
	}

}